package com.suning.app.snpaasadmin.operation;

import com.suning.app.snpaasadmin.dao.AppEnvironmentMapper;
import com.suning.app.snpaasadmin.dao.CfAppMapper;
import com.suning.app.snpaasadmin.pojo.AppEnvironment;
import com.suning.app.snpaasadmin.pojo.AppEnvironmentExample;
import com.suning.app.snpaasadmin.pojo.CfApp;
import com.suning.app.snpaasadmin.pojo.CfAppExample;
import com.suning.app.snpaasadmin.util.Constants;
import com.suning.app.snpaasadmin.util.DbContextHolder;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AppEnvironmentDBOperation {
    
    @Autowired
    private AppEnvironmentMapper appEnvironmentMapper;
    
    @Autowired
    private CfAppMapper cfAppMapper;
    
	public CfApp getApp(String instanceId) {
	    DbContextHolder.setDbType(Constants.DATASOURCE_SERVER);
	    CfApp app = new CfApp();
	    AppEnvironmentExample envExample = new AppEnvironmentExample();
	    envExample.createCriteria().andInstanceIdEqualTo(instanceId);
	    List<AppEnvironment> environments = appEnvironmentMapper.selectByExample(envExample);
	    if(environments != null) {
	        AppEnvironment env = environments.get(0);
	        CfAppExample appExample = new CfAppExample();
	        appExample.createCriteria().andEnvIdEqualTo(env.getId());
	        List<CfApp> apps = cfAppMapper.selectByExample(appExample);
	        if(apps != null) {
	            app = apps.get(0);
	        }
	    }
	    return app;
	}

}
